package com.czz.common.exception;


import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.json.JsonParseException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import com.czz.common.utils.ExceptionUtil;
import com.czz.common.vo.R;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 统一异常处理类
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

	@ExceptionHandler(Exception.class)
	@ResponseBody
	public R error(Exception e){
		e.printStackTrace();
		log.error(ExceptionUtil.getMessage(e));
		return R.error();
	}

	@ExceptionHandler(BadSqlGrammarException.class)
	@ResponseBody
	public R error(BadSqlGrammarException e){
		e.printStackTrace();
		log.error(ExceptionUtil.getMessage(e));
		return R.setResult(ResultCodeEnum.BAD_SQL_GRAMMAR);
	}

	@ExceptionHandler(HttpMessageNotReadableException.class)
	@ResponseBody
	public R error(JsonParseException e){
		e.printStackTrace();
		log.error(ExceptionUtil.getMessage(e));
		return R.setResult(ResultCodeEnum.JSON_PARSE_ERROR);
	}

	@ExceptionHandler(MissingServletRequestParameterException.class)
	@ResponseBody
	public R error(MissingServletRequestParameterException e){
		e.printStackTrace();
		log.error(ExceptionUtil.getMessage(e));
		return R.setResult(ResultCodeEnum.INPUT_ERROR);
	}

	@ExceptionHandler(CustomerException.class)
	@ResponseBody
	public R error(CustomerException e){
		e.printStackTrace();
		log.error(ExceptionUtil.getMessage(e));
		return R.error().message(e.getMessage()).code(e.getCode());
	}
}